// pages/index/index.js
const app = getApp()

Page({
  data: {
    currentTab: 'takeOrder', // takeOrder: 我要接单, buy: 我要买
    takeOrderList: [], // 接单订单列表
    buyCategoryList: [], // 购买分类列表
    userInfo: null,
    hasUserInfo: false
  },

  onLoad() {
    this.checkLoginStatus()
    this.loadTakeOrderData()
    this.loadBuyCategoryData()
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },

  checkLoginStatus() {
    if (!app.globalData.hasUserInfo) {
      wx.redirectTo({
        url: '/pages/login/login'
      })
      return
    }
    
    this.setData({
      userInfo: app.globalData.userInfo,
      hasUserInfo: app.globalData.hasUserInfo
    })
  },

  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({
      currentTab: tab
    })
  },

  loadTakeOrderData() {
    // 模拟Boss直聘式订单数据
    const mockTakeOrderData = [
      {
        id: 1,
        title: '帮忙取快递到宿舍',
        price: 5,
        distance: '500m',
        time: '30分钟前',
        location: '南门快递点',
        destination: '3号楼201',
        urgent: true,
        status: 'pending'
      },
      {
        id: 2,
        title: '代买午餐',
        price: 8,
        distance: '800m',
        time: '1小时前',
        location: '食堂一楼',
        destination: '图书馆3楼',
        urgent: false,
        status: 'pending'
      },
      {
        id: 3,
        title: '打印资料',
        price: 3,
        distance: '300m',
        time: '2小时前',
        location: '文印中心',
        destination: '教学楼101',
        urgent: true,
        status: 'pending'
      }
    ]

    this.setData({
      takeOrderList: mockTakeOrderData
    })
  },

  loadBuyCategoryData() {
    // 模拟闲鱼式分类数据
    const mockCategoryData = [
      {
        id: 1,
        name: '快递代取',
        icon: '🚚',
        count: 23,
        color: '#3498db'
      },
      {
        id: 2,
        name: '外卖代买',
        icon: '🍔',
        count: 15,
        color: '#e74c3c'
      },
      {
        id: 3,
        name: '文档打印',
        icon: '📄',
        count: 8,
        color: '#2ecc71'
      },
      {
        id: 4,
        name: '物品租借',
        icon: '🔑',
        count: 12,
        color: '#f39c12'
      },
      {
        id: 5,
        name: '学习辅导',
        icon: '📚',
        count: 6,
        color: '#9b59b6'
      },
      {
        id: 6,
        name: '其他服务',
        icon: '🔧',
        count: 18,
        color: '#95a5a6'
      }
    ]

    this.setData({
      buyCategoryList: mockCategoryData
    })
  },

  takeOrder(e) {
    const orderId = e.currentTarget.dataset.id
    const order = this.data.takeOrderList.find(item => item.id === orderId)
    
    if (order) {
      wx.showModal({
        title: '确认接单',
        content: `确定要接单"${order.title}"吗？`,
        success: (res) => {
          if (res.confirm) {
            this.handleTakeOrder(orderId)
          }
        }
      })
    }
  },

  handleTakeOrder(orderId) {
    // 模拟接单操作
    wx.showLoading({
      title: '接单中...',
    })

    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '接单成功！',
        icon: 'success'
      })

      // 更新订单状态
      const updatedList = this.data.takeOrderList.map(item => {
        if (item.id === orderId) {
          return { ...item, status: 'taken' }
        }
        return item
      })

      this.setData({
        takeOrderList: updatedList
      })
    }, 1000)
  },

  navigateToCategory(e) {
    const categoryId = e.currentTarget.dataset.id
    const category = this.data.buyCategoryList.find(item => item.id === categoryId)
    
    if (category) {
      wx.showToast({
        title: `进入${category.name}分类`,
        icon: 'none'
      })
      // 实际开发中跳转到对应分类页面
    }
  },

  onPullDownRefresh() {
    this.loadTakeOrderData()
    this.loadBuyCategoryData()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  }
})